<?php
namespace Admin\Model;

use Admin\AppConst\WebConst;

//用户model
class AdminModel extends BaseModel
{
    /**
     * @var AdminModel
     */
    private     static $instance;
    protected   $tableName  = 'admin';
    private     $model;
    private     $where;

    //定义所有的表字段
    private $id                 = 'id';
    private $email              = 'email';
    private $nickName           = 'name';
    private $password           = 'password';
    private $salt               = 'salt';
    private $addtime            = 'addtime';
    private $isDelete           = 'is_delete';
    private $addAdminId         = 'add_admin_id';
    private $addAdminEmail      = 'add_admin_email';
    private $roleId             = 'role_id';

    /**
     * Get the instance of AdminModel.
     * @return AdminModel
     */
    public static function get()
    {
        if (!self::$instance) {
            self::$instance = new AdminModel();
        }
        return self::$instance;
    }

    public function __construct(){
        parent::__construct();
        $this->where    = $this->setDefaultWhere(WebConst::DEFAULT_TYPE_FALSE);
        $this->model    = M($this->tableName);
    }

    /* @function    getAccountByPhone   根据手机号码获取用户舒数量
     * @param       $email      string  手机号码
     * @param       $checkIsDel bool    是否过滤冻结用户
     * @date        2018-07-16
     * @author      zhangle
     * @return      array
     */

    public function getAccountByEmail($email, $checkIsDel){
        $result = WebConst::DEFAULT_TYPE_EMPTY_ARRAY;
        //此处不验证是否禁止登录
        if($checkIsDel == WebConst::DEFAULT_TYPE_FALSE){
            unset($this->where[$this->isDelete]);
        }
        $this->where[$this->email] = $email;

        $this->setFields(array($this->id, $this->email, $this->password, $this->salt, $this->nickName, $this->roleId));

        //查询数据
        $result = $this->model->where($this->where)->field($this->field)->find();
        return $result;
    }


    /* @function    getAccountByUserId   根据手机号码获取用户舒数量
     * @param       $userId     int  用户id
     * @date        2018-07-30
     * @author      zhangle
     * @return      array
     */

    public function getAccountByUserId($userId){
        $result = WebConst::DEFAULT_TYPE_EMPTY_ARRAY;
        //此处不验证是否禁止登录

        $where[$this->id]   = $userId;
        //查询数据
        $result = $this->model->where($this->where)->field($this->field)->find();

        return $result;
    }

    /* @function    updateSalt  每次登录都更新salt, 同时也可以用此方法来验证单点登录
     * @param       $userId         int     userId
     * @param       $iosToken       string  IOS token
     * @param       $androidToken   string  Android  token
     * @date        2018-07-30
     * @author      zhangle
     * @return      bool
     */

    public function updateToken($userId, $iosToken = WebConst::DEFAULT_TYPE_NULL, $androidToken = WebConst::DEFAULT_TYPE_NULL){
        $result = WebConst::DEFAULT_TYPE_TRUE;
        if(is_null($iosToken) && is_null($androidToken)){
            return $result;
        }

        $this->where            = $this->setDefaultWhere(WebConst::DEFAULT_TYPE_FALSE);
        $this->where[$this->id] = $userId;

        $save   = array();
        if(!is_null($iosToken)){
            $save[$this->iosToken]      = $iosToken;
        }

        if(!is_null($androidToken)){
            $save[$this->androidToken]  = $androidToken;
        }

        //查询数据
        $this->model->where($this->where)->save($save);
        return $result;
    }

    /* @function    create      注册
     * @param       $email      string  手机号码
     * @param       $password   string  密码
     * @param       $salt       string  salt
     * @param       $name       string  姓名
     * @param       $roleId     int     权限
     * @date        2018-07-16
     * @author      zhangle
     * @return      int
     */

    public function createAccount($email, $password, $salt, $name, $roleId){
        $result = WebConst::DEFAULT_TYPE_STATUS_FALSE;
        $save   = array(
            $this->email            => $email,
            $this->password         => $password,
            $this->salt             => $salt,
            $this->addtime          => time(),
            $this->nickName         => $name,
            $this->addAdminId       => intval(session(WebConst::USER_SESSION_KEY)['id']),
            $this->addAdminEmail    => empty(session(WebConst::USER_SESSION_KEY)['email']) ? '' : session('user')['email'],
            $this->roleId           => $roleId,
        );

        //插入
        $userId = $this->model->add($save);
        if($userId > WebConst::DEFAULT_TYPE_STATUS_FALSE){
            $result = $userId;
        }
        return $result;
    }

    /* @function    create      修改
     * @param       $id         int     id
     * @param       $name       string  姓名
     * @param       $roleId     int     权限
     * @param       $isUpdatePassword   bool    是否修改密码
     * @param       $password   string  密码
     * @param       $salt       string  salt
     * @date        2018-07-16
     * @author      zhangle
     * @return      bool
     */

    public function editAccount($id, $name, $roleId, $isUpdatePassword, $password = WebConst::DEFAULT_TYPE_EMPTY, $salt = WebConst::DEFAULT_TYPE_EMPTY){
        $save   = array(
            $this->nickName         => $name,
            $this->addAdminId       => intval(session('user')['id']),
            $this->addAdminEmail    => empty(session('user')['email']) ? '' : session('user')['email'],
            $this->roleId           => $roleId,
        );

        if($isUpdatePassword == WebConst::DEFAULT_TYPE_TRUE){
            $save[$this->password]  = $password;
            $save[$this->salt]      = $salt;
        }
        $where  = array(
            $this->id   => $id,
        );
        //更新
        return $this->model->where($where)->save($save);
    }


    /**
     * @function    deleteAccount  删除
     * @param       $id         int  id
     * @author      zhangle
     * @date        2018-08-02
     * @return      bool
     */
    public function deleteAccount($id){
        $where  = array(
            $this->id   => $id,
        );
        $save   = array(
            $this->isDelete => WebConst::DEFAULT_TYPE_DELETE_TRUE,
        );
        //更新
        return $this->model->where($where)->save($save);
    }


    /**
     * @function    getList     获取所有用户
     * @author      zhangle
     * @date        2018-08-02
     * @return      array
     */
    public function getList(){
        return $this->model->select();
    }
    
}